library DivineShieldAbility initializer Init requires entities, utils, DivineShieldBuff

    globals
        public integer ABILITY_ID = 'AHds'
    endglobals

    /**
    * 神圣护甲（无敌）
    */

    private function SpellEnd takes Spell spell returns nothing
        call spell.destroy()
    endfunction

    private function SpellAction takes Spell spell returns nothing
        local Unit ud = Unit.get(spell.m_unit)
        local DivineShieldBuff bfd = DivineShieldBuff.create()

        call bfd.setDuration(spell.m_timeDurative)

        call ud.addBuff(bfd.m_buff)
    endfunction

    private function anon_0 takes nothing returns nothing
        local Spell spell = GetTimerData(GetExpiredTimer())

        call SpellEnd(spell)
    endfunction

    private function SpellEffect takes nothing returns nothing
        local Spell spell
        local integer level

        if not (IsWidgetAlive(SpellEvent.CastingUnit)) then
            return
        endif

        set spell = Spell.create(SpellEvent.CastingUnit, ABILITY_ID)
        set level = GetUnitAbilityLevel(spell.m_unit, ABILITY_ID)

        if level == 1 then
            set spell.m_timeDurative = 15
        elseif level == 2 then
            set spell.m_timeDurative = 30
        elseif level == 3 then
            set spell.m_timeDurative = 45
        endif
        
        call SpellAction(spell)

        call TimerStart(spell.getTimer(), spell.m_timeDurative, false, function anon_0)
    endfunction

    private function SpellChannel takes nothing returns nothing
    endfunction

    private function Init takes nothing returns nothing
        //call RegisterSpellChannelResponse(ABILITY_ID, SpellChannel)
        call RegisterSpellEffectResponse(ABILITY_ID, SpellEffect)
    endfunction

endlibrary
